//
//  WS.m
//  LocationTest
//
//  Created by MAC08 on 07/05/12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "WS.h"
#import "JSONKit.h"
#define BaseURL "http://appask17268.cloudapp.net/"
@implementation WS
@synthesize perseResults;
-(int) sendDataToServer:(NSMutableDictionary *)dic
{
	
	
	if([AppDel networkAvailable])
	{
		
		NSMutableURLRequest *request;
//		NSMutableDictionary *dicValue=nil;
		NSString *str =@"";
		NSArray *arraykeys = [dic allKeys];
		for (int i =0; i < [arraykeys count]; i++) {
			str = [NSString stringWithFormat:@"%@&%@=%@",str,[arraykeys objectAtIndex:i],[dic objectForKey:[arraykeys objectAtIndex:i]]];
		}
		if([str length] > 0) 
			str = [str substringFromIndex:1];
		
		//request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:[[NSString stringWithFormat:@"http://appask17268.cloudapp.net/?%@",str] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
		request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:[[NSString stringWithFormat:@"http://www.quantuminfoways.com/post.php?%@",str] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];

		[request setTimeoutInterval:20];
		[request setHTTPMethod: @"POST"];
		NSURLResponse *response=nil;
		NSError *err=nil;
		NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
		NSString *str1=@"";
		//NSArray *arrayVal;
		//int count;
		if(data!=nil)
		{
			str1 = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
			NSArray *arrayVal=[str1 objectFromJSONString];
//			dicValue=[arrayVal objectAtIndex:0] ;
		}
		else 
		{
			return -1;	
		}
	    [str1 release];
		[request release];
		return 	1;		
	}
	else 
	{
		UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:@"Internet is not available \n Please try after some time" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[alert show];
		[alert release];		
	}
	return -1;
	
}


//#pragma mark -
//#pragma mark Connection Methods
//
//-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
//{
// 	[webData setLength: 0];
//}
//
//-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
//{
//	[webData appendData:data];
//}
//
//-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
//{
//	[connection release];
//	[webData release];
//	UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:@"Please try later, server is not responding now" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil,nil];
//	[alert show];
//	[alert release];
//	
//}
//
//-(void)connectionDidFinishLoading:(NSURLConnection *)connection
//{
//	
//	if(xmlParser)
//	{
//		[xmlParser release];
//		xmlParser = nil;
//	}
//	xmlParser = [[NSXMLParser alloc] initWithData:webData];
//	[xmlParser setDelegate:self];
//	[xmlParser setShouldResolveExternalEntities:YES];
//	[xmlParser parse];
//	
//	[connection release];
//	[webData release];
//	
//}
//
//#pragma mark -
//#pragma mark - Parse Delegate Function 
//-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName
//   attributes: (NSDictionary *)attributeDict
//{
//	
//	if([elementName isEqualToString:@"<<ResultTag>>"])
//	{
//		if(!perseResults)
//		{
//			self.perseResults = [[NSMutableString alloc] init];
//		}
//		else 
//		{
//			self.perseResults = [NSMutableString stringWithString:@""];
//		}	
//		recordResults = TRUE;
//	}
//}
//-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
//{
//	if(recordResults)
//	{
//		[self.perseResults appendString:string];
//	}	
//}
//-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
//{
//	
//	if([elementName isEqualToString:@"<<ResultTag>>"])
//	{
//		NSString *strResult=self.perseResults;
//		
//		recordResults=FALSE;
//			
//	}
//}

@end
